package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.MessageDialog;
import com.kongzue.dialog.v3.WaitDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.DateDetailsContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.DateDetailsPresenter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.DateAdapter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.DateDetailsAdapter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.ImagViewAdapter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.ImagViewAdapter2;
import com.ninetaildemonfox.zdl.socialproject.utils.GlideUtils;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/27 10:13
 * 功能描述： 我的约会  我的动态
 * 联系方式：1037438704@qq.com
 */
public class DateDetailsActivity extends BaseActivity implements DateDetailsContract {

    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.image_thumbs)
    ImageView image_thumbs;
    @BindView(R.id.image_comment)
    ImageView image_comment;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_name)
    TextView textName;
    @BindView(R.id.text_time)
    TextView textTime;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.recyclerViewItem)
    RecyclerView recyclerViewItem;
    @BindView(R.id.recyclerViewImageView)
    RecyclerView recyclerViewImageView;
    @BindView(R.id.recyclerViewTIV)
    RecyclerView recyclerViewTIV;
    @BindView(R.id.image_head)
    ImageView imageHead;
    @BindView(R.id.text_identification)
    TextView textIdentification;
    @BindView(R.id.text_appointment_time)
    TextView text_appointment_time;
    @BindView(R.id.text_expect_ids)
    TextView text_expect_ids;
    @BindView(R.id.text_city)
    TextView text_city;
    @BindView(R.id.text_remarks)
    TextView text_remarks;
    @BindView(R.id.pingjiaRecyclerView)
    RecyclerView pingjiaRecyclerView;
    @BindView(R.id.ll_gone_2)
    LinearLayout ll_gone_2;
    @BindView(R.id.text_my_registration)
    TextView text_my_registration;
    @BindView(R.id.ll_gone_1)
    LinearLayout ll_gone_1;
    private DateDetailsAdapter dateDetailsAdapter;
    private int count;
    private String order_id;
    private DateDetailsPresenter presenter;
    private ImagViewAdapter imagViewAdapter;
    private ImagViewAdapter2 imagViewAdapter2;
    private String type;
    private DateAdapter dateAdapter;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_date_details;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
//        ChatActivity.start(me, member_id + "m", 1);
        initView();
        initData();
    }

    private void initData() {

    }

    private void initView() {
        Bundle extras = this.getIntent().getExtras();
        count = Integer.valueOf(extras.getString("count"));
        order_id = extras.getString("order_id");
        type = extras.getString("type");
        Logger.d("==================" + order_id);
        // 约会类型 2我的动态 1我的约会
        if (count == 1) {
            textCenter.setText("约会详情");
        } else {
            textCenter.setText("我的动态");
            //        text_my_registration    羽毛
        }
        textRight.setText("删除动态");
        textRight.setVisibility(View.VISIBLE);
        presenter = new DateDetailsPresenter(me, this);


        pingjiaRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        dateAdapter = new DateAdapter(R.layout.date_item_adp);
        pingjiaRecyclerView.setAdapter(dateAdapter);
        imageleftFinish.setVisibility(View.VISIBLE);
        recyclerViewItem.setLayoutManager(new LinearLayoutManager(this));
        dateDetailsAdapter = new DateDetailsAdapter(R.layout.item_list_date_adp);
        recyclerViewItem.setAdapter(dateDetailsAdapter);
        recyclerViewImageView.setLayoutManager(new GridLayoutManager(this, 4));
        recyclerViewTIV.setLayoutManager(new GridLayoutManager(this, 6));
        imagViewAdapter = new ImagViewAdapter(R.layout.imageview);
        recyclerViewImageView.setAdapter(imagViewAdapter);
        imagViewAdapter2 = new ImagViewAdapter2(R.layout.imageview2);
        recyclerViewTIV.setAdapter(imagViewAdapter2);

        imagViewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<Map<String, String>> data = imagViewAdapter.getData();
                PicturePreviewActivity.start(me, data, position, 2, "");
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        // 我的动态 2我的约会
        http();
    }

    private void http() {
        if (type.equals("1")) {
            //发布约会
            presenter.dynamicInfo(token, order_id, 1);
        } else {
            //1发布动态
            presenter.orderInfo(token, order_id, 2);
        }
    }

    @OnClick({R.id.image_left_finish, R.id.text_right, R.id.text_my_registration})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            case R.id.text_right:
                //立即开通
                MessageDialog.show(me, "", "确定要删除" + textCenter.getText().toString() + "吗?", "确认", "取消")
                        .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                            @Override
                            public boolean onClick(BaseDialog baseDialog, View v) {
                                WaitDialog.show(me, "数据加载中");
                                presenter.delDynamic(token, order_id);
                                return false;
                            }
                        });
                break;
            case R.id.text_my_registration:
                if (status.equals("2")) {
                    ToastUtil.show("已结束");
                    return;
                }

                //结束报名
                MessageDialog.show(me, "是否结束报名！", "", "确认", "取消")
                        .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                            @Override
                            public boolean onClick(BaseDialog baseDialog, View v) {
                                presenter.endOrder(token, order_id, 1);
                                return false;
                            }
                        });
                break;
            default:
        }
    }

    String status;

    @Override
    public void ateSuccess(Map<String, String> data, int type) {
        // 1我的动态 2我的约会
        if (type == 1) {

        } else {
            Drawable drawable;
            text_my_registration.setVisibility(View.VISIBLE);
            status = data.get("status");
            if (status.equals("2")) {
                drawable = me.getResources().getDrawable(R.mipmap.ic_list_sign_no);
                text_my_registration.setText("已结束(" + data.get("baoming_count") + ")");
            }else {
                drawable = me.getResources().getDrawable(R.mipmap.ic_list_sign_end);
                text_my_registration.setText("结束报名(" + data.get("baoming_count") + ")");
            }
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            text_my_registration.setCompoundDrawables(null, drawable, null, null);
        }

        Log.d("ateSuccess", "==================" + data.toString());
        //动态接口的数据
        //头像
        GlideUtils.glideFille(me, data.get("head_pic"), imageHead);
        //名字
        textName.setText(data.get("nickname"));
        // 时间
        textTime.setText("发布于" + data.get("create_time"));

//        textIdentification
        // 是否认证 1否 2是
        if (getSex.equals("1")) {
            if (getVip.equals("1")) {
                textIdentification.setText("非会员");
                textIdentification.setTextColor(me.getResources().getColor(R.color.white));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg_false);
            } else {
                textIdentification.setText("会员");
                textIdentification.setTextColor(me.getResources().getColor(R.color.white));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg);
            }

        } else {
            if (getIdentification.equals("1")) {
                textIdentification.setText("未认证");
                textIdentification.setTextColor(getResources().getColor(R.color.text_black));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg_false);
            } else {
                textIdentification.setText("已认证");
                textIdentification.setTextColor(getResources().getColor(R.color.white));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg);
            }
        }


        ArrayList<Map<String, String>> pics = JSONUtils.parseKeyAndValueToMapList(data.get("pics"));
        if (pics == null) {
        } else {
            imagViewAdapter.setNewData(pics);
        }

        ArrayList<Map<String, String>> head_pic_good = JSONUtils.parseKeyAndValueToMapList(data.get("head_pic_good"));
        if (head_pic_good == null || head_pic_good.size() == 0) {
            image_thumbs.setImageResource(R.mipmap.ic_give_the_thumbs_up_true);
//            image_thumbs.setImageResource(R.mipmap.ic_give_the_thumbs_up_false);
        } else {
            imagViewAdapter2.setNewData(head_pic_good);
            image_thumbs.setImageResource(R.mipmap.ic_give_the_thumbs_up_true);
        }
//        //评价
        ArrayList<Map<String, String>> pingjia = JSONUtils.parseKeyAndValueToMapList(data.get("pingjia"));
        if (pingjia == null || pingjia.size() == 0) {
//            image_comment.setImageResource(R.mipmap.ic_comment_no);
            image_comment.setImageResource(R.mipmap.ic_comment);
        } else {
            dateAdapter.setNewData(pingjia);
            image_comment.setImageResource(R.mipmap.ic_comment);
        }
        ArrayList<Map<String, String>> enlist = JSONUtils.parseKeyAndValueToMapList(data.get("enlist"));
        if (enlist == null || enlist.size() == 0) {
        } else {
            dateDetailsAdapter.setNewData(enlist);
        }

        if (TextUtils.isEmpty(data.get("appointment_time")) || TextUtils.isEmpty(data.get("appointment_am"))) {
            text_appointment_time.setVisibility(View.GONE);
        } else {
            // 约会时间             // 约会日期
            text_appointment_time.setText("约会时间:"+data.get("appointment_time") + data.get("appointment_am"));
            text_appointment_time.setVisibility(View.VISIBLE);
        }

        if (TextUtils.isEmpty(data.get("expect_ids"))) {
            text_expect_ids.setVisibility(View.GONE);
        } else {
            // 期望对象
            text_expect_ids.setText("期望对象：" + data.get("expect_ids"));
            text_expect_ids.setVisibility(View.VISIBLE);
        }

        if (TextUtils.isEmpty(data.get("city"))) {
            text_city.setVisibility(View.GONE);
        } else {
            // 期望对象
            text_city.setText("约会地点 ：" + data.get("city"));
            text_city.setVisibility(View.VISIBLE);
        }


        text_remarks.setText("备注说明 :" + data.get("remarks"));
    }

    @Override
    public void Failed(String msg) {

    }

    @Override
    public void showLoading() {
        WaitDialog.show(me, "数据加载中");
    }

    @Override
    public void delDynamic() {
        finish();
    }

    @Override
    public void endBaoming() {
        http();
    }

    public static void start(Context context, String count, String order_id) {
        Bundle bundle = new Bundle();
        bundle.putString("count", count);
        bundle.putString("order_id", order_id);
        Intent intent = new Intent(context, DateDetailsActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public static void start(Context context, String count, String order_id, String type) {
        Bundle bundle = new Bundle();
        bundle.putString("count", count);
        bundle.putString("order_id", order_id);
        bundle.putString("type", type);
        Intent intent = new Intent(context, DateDetailsActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

}
